package com.example.administrator.nl.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.nl.R;
import com.example.administrator.nl.adapter.CartAdapter;
import com.example.administrator.nl.bean.CartBean;
import com.example.administrator.nl.util.LogUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 购物车
 */
public class CartFragment extends Fragment {
    private String TAG="CartFragment";
    private Unbinder unbinder;

    // 垃圾箱
    @BindView(R.id.iv_dustbin_cart)
    ImageView iv_dustbin_cart;

    // 当购物车有商品时，显示这个
    @BindView(R.id.view_bk_cart)
    View view_bk_cart;

    // 当购物车没有商品时，显示这个
    @BindView(R.id.tv_bk_cart)
    TextView tv_bk_cart;


    @BindView(R.id.recycler_view)
    RecyclerView recycler_view;

    // 如果有商品则显示，没有则隐藏
    @BindView(R.id.ll_cart)
    LinearLayout ll_cart;

    // 结算
    @BindView(R.id.tv_settlement_cart)
    TextView tv_settlement_cart;

    List<CartBean> cartBeanList;

    CartAdapter cartAdapter;

    public CartFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_cart, container, false);
        unbinder = ButterKnife.bind(this, view);
        LogUtil.Log_D(TAG,"onCreateView");
        init();
        recycler_view.setLayoutManager(new LinearLayoutManager(getActivity()));
        cartAdapter = new CartAdapter(R.layout.item_cart_recycle, cartBeanList);
        recycler_view.setAdapter(cartAdapter);

        return view;
    }

    private void init() {
        cartBeanList = getData();
        // 当购物车有商品时
        if (cartBeanList.size() > 0) {
            view_bk_cart.setVisibility(View.VISIBLE);
            tv_bk_cart.setVisibility(View.GONE);
            recycler_view.setVisibility(View.VISIBLE);
            ll_cart.setVisibility(View.VISIBLE);
        } else {
            view_bk_cart.setVisibility(View.GONE);
            tv_bk_cart.setVisibility(View.VISIBLE);
            recycler_view.setVisibility(View.GONE);
            ll_cart.setVisibility(View.GONE);

        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtil.Log_D(TAG,"onDestroy");
        if (null != unbinder) {
            unbinder.unbind();
        }
    }

    public List<CartBean> getData() {
        cartBeanList = new ArrayList<>();
        cartBeanList.add(new CartBean());
        cartBeanList.add(new CartBean());
        cartBeanList.add(new CartBean());
        cartBeanList.add(new CartBean());
        cartBeanList.add(new CartBean());
        cartBeanList.add(new CartBean());
        return cartBeanList;
    }
}
